package com.wangxiang.shop.product.service.impl;

import com.wangxiang.common.utils.DataEntity;
import com.wangxiang.shop.product.dao.SpuDao;
import com.wangxiang.shop.product.entity.SpuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxiang.common.utils.PageUtils;
import com.wangxiang.common.utils.Query;

import com.wangxiang.shop.product.dao.ShopTypeDao;
import com.wangxiang.shop.product.entity.ShopTypeEntity;
import com.wangxiang.shop.product.service.ShopTypeService;


@Service("shopTypeService")
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeDao, ShopTypeEntity> implements ShopTypeService {
    @Autowired
    private ShopTypeDao shopTypeDao;
    @Autowired
    private SpuDao spuDao;

    @Override
    public List<ShopTypeEntity> shopList() {
        List<ShopTypeEntity> shopList = shopTypeDao.selectList(null);
        ArrayList<ShopTypeEntity> shopTypeEntities = new ArrayList<>();
        ArrayList<ShopTypeEntity> one = new ArrayList<>();
        ArrayList<ShopTypeEntity> two = new ArrayList<>();
        ArrayList<ShopTypeEntity> three = new ArrayList<>();
        for (int i = 0; i < shopList.size(); i++) {

            //第一级
            if (shopList.get(i).getLevel()==0){
                one.add(shopList.get(i));
            }
            //第二级
            if (shopList.get(i).getLevel()==1){
                two.add(shopList.get(i));
            }
            //第一级
            if (shopList.get(i).getLevel()==2){
                three.add(shopList.get(i));
            }
        }
        for (int i = 0; i < one.size(); i++) {
            ShopTypeEntity oneEntity = new ShopTypeEntity();
            oneEntity = one.get(i);

            ArrayList<ShopTypeEntity> twoList = new ArrayList<>();
            for (int j = 0; j < two.size(); j++) {
                ArrayList<ShopTypeEntity> threeList = new ArrayList<>();
                for (int k = 0; k < three.size(); k++) {
                   if (two.get(j).getId()==three.get(k).getParentId()){
                       threeList.add(three.get(k));
                   }
                }
                two.get(j).setChildren(threeList);

                if (oneEntity.getId()==two.get(j).getParentId()){
                    twoList.add(two.get(j));
                }

            }
            oneEntity.setChildren(twoList);

            shopTypeEntities.add(oneEntity);


        }






        return shopTypeEntities;
    }

    @Override
    public List<SpuEntity> spuByType(String type) {
        return null;
    }

    @Override
    public List<ShopTypeEntity> twoType() {
        List<ShopTypeEntity> shopTypeEntities = shopTypeDao.selectList(null);
        List<ShopTypeEntity> list = new ArrayList<>();
        for (ShopTypeEntity shopTypeEntity : shopTypeEntities) {
            if (shopTypeEntity.getLevel()==1){
                list.add(shopTypeEntity);
            }
        }
        return list;
    }

    @Override
    public List<ShopTypeEntity> threeType(int parentId) {
        List<ShopTypeEntity> shopTypeEntities = shopTypeDao.selectList(null);
        List<ShopTypeEntity> list = new ArrayList<>();
        for (ShopTypeEntity shopTypeEntity : shopTypeEntities) {
            if (shopTypeEntity.getParentId()==parentId){
                list.add(shopTypeEntity);
            }

        }
        return list;
    }

    @Override
    public List<ShopTypeEntity> threebar() {
        List<ShopTypeEntity> shopTypeEntities = shopTypeDao.selectList(null);
        List<ShopTypeEntity> list = new ArrayList<>();
        for (ShopTypeEntity shopTypeEntity : shopTypeEntities) {
            if (shopTypeEntity.getLevel()==2){
                list.add(shopTypeEntity);
            }
        }
        return list;
    }

    @Override
    public List<HashMap<String, Object>> threeList() {
        List<ShopTypeEntity> shopTypeEntities = shopTypeDao.listBylevel(2);

        List<HashMap<String, Object>> hashMaps = new ArrayList<>();
        for (int i = 0; i < shopTypeEntities.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("name" , shopTypeEntities.get(i).getName());
            map.put("chooseActiv" , i);
            map.put("image",shopTypeEntities.get(i).getImage());
            hashMaps.add(map);
        }
        return hashMaps;
    }

    @Override
    public int updateTypeRange(String type, int range) {
        return 0;
    }

    @Override
    public List<DataEntity> shopTypeData() {
        ArrayList<DataEntity> dataEntities = new ArrayList<>();
        //查出所有一级分类

        List<ShopTypeEntity> shopTypeEntities = shopTypeDao.listBylevel(0);
        int sum  = 0;
        //查二级分类
        for (ShopTypeEntity shopTypeEntity : shopTypeEntities) {
            List<ShopTypeEntity> children = shopTypeDao.getChildren(shopTypeEntity.getId());
            int count = 0;
            DataEntity dataEntity = new DataEntity();
            for (ShopTypeEntity child : children) {

                List<ShopTypeEntity> children1 = shopTypeDao.getChildren(child.getId());
                for (ShopTypeEntity typeEntity : children1) {
                    //使用索引
                    int typeData = spuDao.getTypeData(typeEntity.getId());
                    count = count+typeData;
                    sum = sum+typeData;
                }

            }
            dataEntity.setName(shopTypeEntity.getName());
            dataEntity.setValue(count);
            dataEntities.add(dataEntity);
        }
//        System.out.println(sum);
       return dataEntities;
    }

    @Override
    public List<DataEntity> twoTypeData() {
        ArrayList<DataEntity> dataEntities = new ArrayList<>();
        List<ShopTypeEntity> shopTypeEntities = shopTypeDao.listBylevel(1);
        for (ShopTypeEntity shopTypeEntity : shopTypeEntities) {
            List<ShopTypeEntity> children = shopTypeDao.getChildren(shopTypeEntity.getId());
            int count = 0;
            for (ShopTypeEntity child : children) {
                int typeData = spuDao.getTypeData(child.getId());
                count = typeData+count;

            }
            DataEntity dataEntity = new DataEntity();
            dataEntity.setName(shopTypeEntity.getName());
            dataEntity.setValue(count);
            dataEntities.add(dataEntity);
        }
        return dataEntities;
    }

    @Override
    public List<DataEntity> treeTypeData() {
        ArrayList<DataEntity> dataEntities = new ArrayList<>();
        List<ShopTypeEntity> shopTypeEntities = shopTypeDao.listBylevel(2);
        for (ShopTypeEntity shopTypeEntity : shopTypeEntities) {
            int typeData = spuDao.getTypeData(shopTypeEntity.getId());
            DataEntity dataEntity = new DataEntity();
            dataEntity.setName(shopTypeEntity.getName());
            dataEntity.setValue(typeData);
            dataEntities.add(dataEntity);
        }
        return dataEntities;
    }

    @Override
    public List<DataEntity> wareTotalData() {
        ArrayList<DataEntity> dataEntities = new ArrayList<>();
        //查出所有一级分类

        List<ShopTypeEntity> shopTypeEntities = shopTypeDao.listBylevel(0);
        int sum  = 0;
        //查二级分类
        for (ShopTypeEntity shopTypeEntity : shopTypeEntities) {
            List<ShopTypeEntity> children = shopTypeDao.getChildren(shopTypeEntity.getId());
            int count = 0;
            DataEntity dataEntity = new DataEntity();
            for (ShopTypeEntity child : children) {
                List<ShopTypeEntity> children1 = shopTypeDao.getChildren(child.getId());
                for (ShopTypeEntity typeEntity : children1) {
//                    System.out.println(typeEntity);

                    try {
                        Integer i = spuDao.baseWareData(typeEntity.getId());
                        count=count+i;
                    }catch (NullPointerException e){
                        System.out.println("抛出异常商品类型"+typeEntity.getId());
                        e.printStackTrace();
                    }

//                    System.out.println(i+"////"+typeEntity.getId());
                }

            }
            dataEntity.setName(shopTypeEntity.getName());
            dataEntity.setValue(count);
            dataEntities.add(dataEntity);
        }
//        System.out.println(sum);
        return dataEntities;

    }

    @Override
    public List<DataEntity> shopThreeWare() {
        ArrayList<DataEntity> list = new ArrayList<>();
        List<ShopTypeEntity> shopTypeEntities = shopTypeDao.listBylevel(2);
        for (ShopTypeEntity shopTypeEntity : shopTypeEntities) {
            DataEntity dataEntity = new DataEntity();
            try {
                Integer ware = spuDao.baseWareData(shopTypeEntity.getId());
                dataEntity.setName(shopTypeEntity.getName());
                dataEntity.setValue(ware);
                list.add(dataEntity);
            }catch (NullPointerException e){
                System.out.println("抛出异常商品类型"+shopTypeEntity.getName());
                e.printStackTrace();
            }

        }
        return list;
    }


}